package com.fengcone.lucene.analyzer;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MtyStringUtils {
	public static List<String> splitAndReturnDelimiters(Pattern pattern, String str) {
		Matcher matcher = pattern.matcher(str);
		List<String> result = new ArrayList<String>();
		int strLen = str.length();
		int lastMatchIdx = 0;
		while (matcher.find()) {
			int start = matcher.start();
			int end = matcher.end();
			String ds = matcher.group();

			if (lastMatchIdx != start) {
				String leftS = str.substring(lastMatchIdx, start);
				result.add(leftS);
			}

			result.add(ds);

			lastMatchIdx = end;
		}

		if (lastMatchIdx < strLen) {
			result.add(str.substring(lastMatchIdx, strLen));
		}
		return result;

	}
}
